package com.jltx

import org.gradle.BuildListener
import org.gradle.BuildResult
import org.gradle.api.Plugin
import org.gradle.api.initialization.Settings
import org.gradle.api.invocation.Gradle

/**
 * @author aidenquan* Created by junlintianxia on 2020/08/20.
 */
class SettingPlugin implements Plugin<Settings> {
    @Override
    void apply(Settings settings) {
        println ">>SettingPlugin插件开始配置"
        settings.gradle.addBuildListener(mBuildListener)
        settings.gradle.settingsEvaluated {
            println ">>gradle.settingsEvaluated"
        }

        settings.gradle.beforeProject {
            println ">>gradle.beforeProject"
        }

        settings.gradle.afterProject {
            println ">>gradle.afterProject"
        }

        settings.gradle.projectsLoaded {
            println ">>gradle.projectsLoaded"
        }

        settings.gradle.projectsEvaluated {
            println ">>gradle.projectsEvaluated"
        }

        settings.gradle.taskGraph.whenReady {
            println ">>gradle.taskGraph.whenReady"
        }

        settings.gradle.taskGraph.beforeTask {
            println ">>gradle.taskGraph.beforeTask"
        }

        settings.gradle.taskGraph.afterTask {
            println ">>gradle.taskGraph.afterTask"
        }

        settings.gradle.buildFinished {
            println ">>gradle.buildFinished"
        }

        settings.gradle.allprojects {
            project ->
                if (project == project.rootProject) {
                    println ">>com.jltx.SettingPlugin 调用project.name " + project.name
                    new RootPluginProject().apply(project)
                } else {
                    println ">>com.jltx.SettingPlugin 调用project.name " + project.name
                    new SubPluginProject().apply(project)
                }
        }
    }
    private BuildListener mBuildListener = new BuildListener() {
        @Override
        void buildStarted(Gradle gradle) {
            println ">>开始构建"
        }

        @Override
        void settingsEvaluated(Settings settings) {
            println ">>settings评估完成（settins.gradle中代码执行完毕）"
        }

        @Override
        void projectsLoaded(Gradle gradle) {
            println '项目结构初始化阶段完成（初始化阶段结束）'
            println '初始化结束，可访问根项目：' + gradle.gradle.rootProject
        }

        @Override
        void projectsEvaluated(Gradle gradle) {
            println '所有项目评估完成（配置阶段结束）'
        }

        @Override
        void buildFinished(BuildResult result) {
            println '构建结束 '
        }
    }
}

